﻿using AutoMapper;
using B.S.BaseData.Domain.Entities;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infrastructure.Interfaces;
using B.S.BaseData.Read.API.Command.RoleMenuCommand;
using MediatR;

namespace B.S.BaseData.Read.API.CommandHandler.RoleMenuHandler
{
    public class FindMenuHandler : IRequestHandler<FindMenuCommand, APIResult<object>>
    {
        private readonly IBaseRepository<Role> roleRep;
        private readonly IBaseRepository<Menu> menuRep;
        private readonly IBaseRepository<RoleMenu> rmRep;
        private readonly IMapper mapper;

        public FindMenuHandler(IBaseRepository<Role> roleRep,IBaseRepository<Menu> menuRep,IBaseRepository<RoleMenu> rmRep,IMapper mapper)
        {
            this.roleRep = roleRep;
            this.menuRep = menuRep;
            this.rmRep = rmRep;
            this.mapper = mapper;
        }
        public Task<APIResult<object>> Handle(FindMenuCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            var list = from a in roleRep.GetAll()
                       join b in rmRep.GetAll() on a.RoleId equals b.RoleId
                       join c in menuRep.GetAll() on b.MenuId equals c.MenuId
                       where a.RoleId == request.RoleId
                       select new
                       {
                           
                           c.MenuId,
                           c.MenuName,
                       };
            result.Code = APIEnum.Success;
            result.Msg = "查询成功";
            result.Data = list;

            return Task.FromResult(result);
        }
    }
}
